//
//  MTRedpacketTextFieldCell.swift
//  MT
//
//  Created by ooops on 2020/5/15.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Foundation
import Eureka
import MTCategory

public final class MTRedpacketTextFieldRow: MTRedpacketTextFieldCellEditRow, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

open class MTRedpacketTextFieldCellEditRow: MTRedpacketTextFieldCellRow {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

open class MTRedpacketTextFieldCell: Cell<String>, CellType {

    public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    var tipText: String? {
        didSet {
            self.tipLabel.text = tipText
        }
    }
    
    var keyboardType: UIKeyboardType? = .default {
        didSet {
            contentTextField.keyboardType = keyboardType ?? .default
        }
    }
    
    func setupAccessoryView(text: String?, image: String?) {
        if text != nil {
            self.customAccessorTextyView.text = text
            customAccessoryView.isHidden = true
            customAccessorTextyView.isHidden = false
            return
        }
        if image != nil {
            customAccessorTextyView.isHidden = true
            customAccessoryView.isHidden = false
            customAccessoryView.image = UIImage(named: image!)
            return
        }
    }
    
    var placeholder: String? {
        didSet {
            if let p = placeholder {
                contentTextField.attributedPlaceholder = NSAttributedString(string: p, attributes: [NSAttributedString.Key.foregroundColor : UIColor.text.x9b9b9b, NSAttributedString.Key.font : PingFangSCMediumFont(14)])
            }
        }
    }
    
    lazy var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 8
        return v
    }()
    
    /// 显示模式
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "名称"
        l.textAlignment = .left
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var customAccessoryView: UIImageView = {
            let imageV = UIImageView(image: UIImage(named: "icon_right_gary"))
            return imageV
    }()
    
    lazy var customAccessorTextyView: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        return l
    }()
    
    lazy var contentTextField: UITextField = {
        let txf = UITextField()
        txf.textColor = UIColor.text.x9b9b9b
        txf.font = PingFangSCMediumFont(14)
        txf.textAlignment = .right
        return txf
    }()
    
    open override func setup() {
        super.setup()
        backgroundColor = UIColor(hex: 0xf4f4f4)
        selectionStyle = .none
        textLabel?.isHidden = true
        detailTextLabel?.isHidden = true
        editingAccessoryType =  .none
        
        contentView.addSubview(bgView)
        bgView.addSubview(tipLabel)
        bgView.addSubview(contentTextField)
        bgView.addSubview(customAccessoryView)
        bgView.addSubview(customAccessorTextyView)
        
        makeConstraint()
        
        self.contentTextField.rx.text.orEmpty.changed.subscribe { text in
            let value: String = text.element!
            
            if value.count == 0 {
                self.contentTextField.text = ""
            } else {
//                if self.maxTextLength != 0 && value.count > self.maxTextLength {
//                    self.contentTextField.text = value.mySubString(to: self.maxTextLength)
//                }
                self.row.value = self.contentTextField.text
            }
            
            self.row.value = self.contentTextField.text
            /// 更新 cell
            //self.row.updateCell()
        }.disposed(by: globalDisposedBag)
    }
    
    private func makeConstraint() {
        bgView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(8)
            make.right.equalToSuperview().offset(-8)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.width.greaterThanOrEqualTo(30)
        }
        
        contentTextField.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(tipLabel.snp.right)
            make.right.equalTo(customAccessoryView.snp.left).offset(-9)
        }

        customAccessoryView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalTo(tipLabel)
            make.width.height.equalTo(15)
        }
        
        customAccessorTextyView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalTo(tipLabel)
            make.width.height.equalTo(15)
        }
    }

    deinit {
//        datePicker.removeTarget(self, action: nil, for: .allEvents)
    }

    open override func update() {
        super.update()
        if row.value != nil {
            contentTextField.text = row.displayValueFor?(row.value)
        }
    }

    open override func didSelect() {
        super.didSelect()
        row.deselect()
    }
    
    open override func cellCanBecomeFirstResponder() -> Bool {
        return canBecomeFirstResponder
    }

    override open var canBecomeFirstResponder: Bool {
        return row.isDisabled
    }
}

open class MTRedpacketTextFieldCellRow: Row<MTRedpacketTextFieldCell>, NoValueDisplayTextConformance {

    open var noValueDisplayText: String? = nil

    required public init(tag: String?) {
        super.init(tag: tag)
        displayValueFor = { [unowned self] value in
            guard let val = value else { return nil }
            return val
        }
    }
}

